<template>
  <div>
    <el-button type="primary" @click="modalFun('add',{})">添加角色</el-button>
    <el-table
    :data="roleList"
    style="width: 100%">
    <el-table-column
      prop="name"
      label="角色名"
      >
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="deleteFun(scope.row._id)" type="text" size="small">删除</el-button>
        <el-button @click="modalFun('auth',scope.row)" type="text" size="small">授权</el-button>
       
      </template>
    </el-table-column>
  </el-table>

  <el-dialog
      :title="modalObj.title"
      :visible.sync="modalObj.isOpen"
      width="50%"
      >
    <template v-if="modalObj.kind=='add'">
      <el-form :model="ruleForm" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          <el-form-item label="角色名称" prop="name"> 
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('form')">立即创建</el-button>
            <el-button @click="resetForm('form')">重置</el-button>
          </el-form-item>
      </el-form>
    </template>
    <template v-if="modalObj.kind=='auth'">
      <MenuAuth :currentData="currentData" @send="getResData"></MenuAuth>
    </template>
     


  </el-dialog>
  </div>
</template>

<script>
import axiosEl from '@/utils/axios';
import MenuAuth from '@/components/MenuAuth.vue';
  export default {
      data(){
          return {
              currentData:{},
              roleList:[],
              modalObj:{
                isOpen:false,
                title:'添加',
                kind:''
              },
              ruleForm:{},
              rules:{
                name: [
                  { required: true, message: '请输入菜单name', trigger: 'blur' },        
              ]
              }
          }
      },
      components:{
        MenuAuth
      },
    methods: {
      // 获取授权子组件给的数据
      getResData(data){
         this.modalObj.isOpen = false;
         this.getAllRoles();
      },
     // 删除某个角色
     deleteFun(id){
        this.$confirm('确认要删除吗', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
         }).then(async ()=>{
          // 确认 删除   调接口
            let resData = await  axiosEl.post('/roles/deleteRoles',{id:id});
            
            if(resData.code==1){
                this.$message({
                  type:'success',
                  message:resData.msg
                })
                this.getAllRoles()

            }


         }).catch(()=>{
          //取消
         })
     },

      modalFun(kind,data){
       
        
        this.modalObj.isOpen = true;
        this.modalObj.title = kind=='add'? '添加角色':'授权';
        this.modalObj.kind = kind;

        this.currentData = data;


      },

     async getAllRoles(){
        let resData = await axiosEl({
          method:'get',
          url:'/roles/findRoles'
        })
        // ?.的作用  resData如果有数据 就读后面的属性值， 如果是undefined就不会执行 也不会报错
        if(resData.code==1){
          this.roleList = resData.data;
        }
        },
        submitForm(){
          this.$refs.form.validate((valid)=>{
              if(!valid){
                  return;
              }
              axiosEl({
                method:'post',
                url:'/roles/addRoles',
                data:this.ruleForm
              }).then((res)=>{
                  this.$message({
                    type: 'success',
                    message: '添加成功'
                  }); 
                  this.getAllRoles();
                  this.modalObj.isOpen = false;
                  this.ruleForm={}
                 
              })
          })
         
        }
    },
    mounted(){
        this.getAllRoles()
    }
  }
</script>

<style lang="scss">
.tag-col {
  display: flex;
 span { margin-left:10px}
}
</style>